package bookshelf.client.book;


import java.util.ArrayList;

import bookshelf.client.BookItemService;
import bookshelf.client.BookItemServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
//import com.google.gwt.user.client.ui.Button;

public class BookController {
	
	private static BookController singleton;
	
	private BookList bl;
	private BookDetail bd;

	final ArrayList<BookItem> booklist = new ArrayList<BookItem>();

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 * リモートサービスプロキシを生成
	 */
	private final BookItemServiceAsync bookItemService = GWT.create(BookItemService.class);

	static public BookController getBookController(){
		if(singleton==null){
			singleton = new BookController();
		}
		
		return singleton;
	}
	
	public void setBookList(BookList bl){
		this.bl = bl;
	}
	
	public void setBookDetail(BookDetail bd){
		this.bd = bd;
	}
	
	public void setBookItemToDetail(BookItem bi){
		bd.setBookItem(bi);
	}
	
	public ArrayList<BookItem> getBooks(){
		
		bookItemService.getBookItemList(new AsyncCallback<ArrayList<BookItem>>() {
			public void onFailure(Throwable caught) {
				// 非同期通信エラー発生時の処理
				caught.printStackTrace();
				booklist.clear();
			}

			// 通信成功した場合の処理
			public void onSuccess(ArrayList<BookItem> result) {
				//ArrayListのクリア
				booklist.clear();
				
				//入れ物詰め替え
				for(int j=0;j<result.size();j++){
					booklist.add(result.get(j));					
				}
			}
		});	
		
		return booklist;
	}
}
